package com.iscreen.core.trading.actions.parsers;

import java.math.BigDecimal;
import java.sql.Date;

import javax.persistence.EntityManager;

import com.iscreen.core.utils.ejb.ServiceLocator;
import com.iscreen.entities.DailyTradeResults;
import com.iscreen.entities.DailyTradeResultsPK;
import com.iscreen.utils.exceptions.IScreenException;

public class TradingResultsParser extends BaseDailyDataParser
{
	private static int START_RECORD_TYPE = 0;
	private static int START_SECURITY_ID = 2;
	private static int START_SECURITY_TYPE = 10; 
	private static int START_SECTOR = 12;
	private static int START_SUB_SECTOR = 14;
	private static int START_TRADING_METHOD = 16;
	private static int START_CLOSING_PRICE = 18;

    @Override
	protected void parseLine(String line) throws IScreenException
    {
    	if (line.startsWith("02"))
    	{
	    	String recordType = line.substring(0,START_SECURITY_ID);
	    	String securityId = line.substring(START_SECURITY_ID,START_SECURITY_TYPE);
	    	String securityType = line.substring(START_SECURITY_TYPE,START_SECTOR);
	    	String sector = line.substring(START_SECTOR,START_SUB_SECTOR);
	    	String subSector = line.substring(START_SUB_SECTOR,START_TRADING_METHOD);
	    	String tradingMethod = line.substring(START_TRADING_METHOD,START_CLOSING_PRICE);
	    	String closingPrice = line.substring(START_CLOSING_PRICE,START_CLOSING_PRICE + 9);
	    	
	    	System.out.println("recordType = " + recordType);
	    	System.out.println("securityId = " + securityId);
	    	System.out.println("securityType = " + securityType);
	    	System.out.println("sector = " + sector);
	    	System.out.println("subSector = " + subSector);
	    	System.out.println("tradingMethod = " + tradingMethod);
	    	System.out.println("closingPrice = " + closingPrice);
	    	
	    	
	    	DailyTradeResultsPK dtrPk = new DailyTradeResultsPK();
	    	dtrPk.setSecurityId(securityId);
	    	dtrPk.setTradeDate(new Date(System.currentTimeMillis()));
	    	DailyTradeResults dtr = new DailyTradeResults();
	    	dtr.setPk(dtrPk);
	    	BigDecimal price = new BigDecimal("100.00");
	    	//dtr.setClosingPrice(price);
	    	EntityManager em = ServiceLocator.getInstance().getEntityManager();
	    	em.persist(dtr);
    	}    	
    }
    
    

}
